package com.yuhang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyBill;
import com.yuhang.entity.HyRepair;
import com.yuhang.entity.HyRepairStream;
import com.yuhang.entity.HyUser;
import com.yuhang.service.HyRepairStreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/hy-repair-stream")
public class HyRepairStreamController {

    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;

    @Autowired
    private HyRepairStreamService hyRepairStreamService;

    @RequestMapping("/showAllRepairStream")
    public Map<String,Object> shoAllRepairStream(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                                 @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                                 @RequestParam(value = "likeusername",defaultValue = "")String likeusername,
                                                 @RequestParam(value = "likerepairname",defaultValue = "")String likerepairname,
                                                 @RequestParam(value = "likedevicename",defaultValue = "")String likedevicename){
        IPage<HyRepairStream> page = hyRepairStreamService.selectByPage1(pageNum, pageSize,likeusername,likerepairname,likedevicename);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }
    @RequestMapping("/showUserRepairStream")
    public Map<String,Object> showUserRepairStream(HttpSession session,
                                                   @RequestParam(value = "page",defaultValue = "1")int pageNum,
                                                   @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                                   @RequestParam(value = "likerepairname",defaultValue = "")String likerepairname,
                                                   @RequestParam(value = "likedevicename",defaultValue = "")String likedevicename){
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
//        List<HyRepairStream> list = hyRepairStreamService.list();
        IPage<HyRepairStream> page = hyRepairStreamService.selectByPage2(pageNum, pageSize,hyUser.getId(),likerepairname,likedevicename);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }
    @RequestMapping("/showRepairStream1")
    public Map<String,Object> showRepairStream1(HttpSession session,
                                                   @RequestParam(value = "page",defaultValue = "1")int pageNum,
                                                   @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                                   @RequestParam(value = "likedevicename",defaultValue = "")String likedevicename){
        HyRepair hyRepair = (HyRepair)session.getAttribute("hyRepair");
        IPage<HyRepairStream> page = hyRepairStreamService.selectByPage3(pageNum, pageSize,likedevicename);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }
    @RequestMapping("/showRepairStream2")
    public Map<String,Object> showRepairStream2(HttpSession session,
                                                   @RequestParam(value = "page",defaultValue = "1")int pageNum,
                                                   @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                                   @RequestParam(value = "likedevicename",defaultValue = "")String likedevicename){
        HyRepair hyRepair = (HyRepair)session.getAttribute("hyRepair");
        IPage<HyRepairStream> page = hyRepairStreamService.selectByPage4(pageNum, pageSize,hyRepair.getId(),likedevicename);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        map.put("msg","");
        return map;
    }

    @RequestMapping("/addRepairStream")
    public Map<String,Object> addRepairStream(HyRepairStream hyRepairStream){
        hyRepairStream.setState(1);
        hyRepairStreamService.save(hyRepairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/addUserRepairStream")
    public Map<String,Object> addUserRepairStream(HttpSession session,HyRepairStream hyRepairStream){
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        hyRepairStream.setUserId(hyUser.getId());
        hyRepairStream.setUserName(hyUser.getName());
        hyRepairStream.setState(1);
        hyRepairStreamService.save(hyRepairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }

    @RequestMapping("/updateRepairStream")
    public Map<String,Object> updateRepairStream(HyRepairStream hyRepairStream){
        hyRepairStreamService.updateById(hyRepairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }

    @RequestMapping("/deleteRepairStream")
    public Map<String,Object> deleteRepairStream(Integer id){
        hyRepairStreamService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/jiedanRepairStream")
    public Map<String,Object> jiedanRepairStream(HttpSession session,Integer id){
        HyRepair hyRepair = (HyRepair)session.getAttribute("hyRepair");
        HyRepairStream hyRepairStream = hyRepairStreamService.getById(id);
        hyRepairStream.setState(2);
        hyRepairStream.setRepairId(hyRepair.getId());
        hyRepairStream.setRepairName(hyRepair.getName());
        hyRepairStreamService.updateById(hyRepairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/weixiuRepairStream")
    public Map<String,Object> weixiuRepairStream(Integer id){
        HyRepairStream hyRepairStream = hyRepairStreamService.getById(id);
        hyRepairStream.setState(3);
        hyRepairStreamService.updateById(hyRepairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/pingjiaRepairStream")
    public Map<String,Object> pingjiaRepairStream(HyRepairStream hyRepairStream){
        HyRepairStream repairStream = hyRepairStreamService.getById(hyRepairStream);
        repairStream.setState(4);
        repairStream.setEvaluate(hyRepairStream.getEvaluate());
        hyRepairStreamService.updateById(repairStream);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }
}
